import { prefix } from './constants';

abstract class Storage {
  static contianer: globalThis.Storage;
  static format(key: string) {
    return prefix + key;
  }
  static get<T>(key: string): T | null {
    let data: any = this.contianer.getItem(this.format(key));
    if (data) {
      try {
        data = JSON.parse(data);
      } catch (e) {}
    }
    return data;
  }
  static get_batch<T extends any[]>(keys: string[]): T {
    return keys.map((key) => this.get(key)) as T;
  }
  static set(key: string, value: any): void {
    this.contianer.setItem(this.format(key), value);
  }
  static set_batch(data: Record<string | number, unknown>): void {
    for (const [key, value] of Object.entries(data)) {
      this.set(key, value);
    }
  }
  static remove(key: string) {
    this.contianer.removeItem(this.format(key));
  }
  static clear() {
    this.contianer.clear();
  }
}
export class LocalStorage extends Storage {
  static contianer = window.localStorage;
}
export class SessionStorage extends Storage {
  static contianer = window.sessionStorage;
}
